﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Text.RegularExpressions;
namespace BitPortal.Business
{
    public static partial class PortalConfigBLL
    {
        public static int Insert(this PortalConfig config)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            //Check Name Valid
            Regex r = new Regex("\\w");
            if (!r.IsMatch(config.Name))
            {
                throw new Exception("Tên cấu hình không đúng định dạng!");
            }
            //Check Exist Name
            if (bitEnt.PortalConfigs.Count(ent => ent.Name == config.Name) > 0)
            {
                throw new Exception("Tên cấu hình đã tồn tại!");
            }
            //
            bitEnt.PortalConfigs.AddObject(config);
            return bitEnt.SaveChanges();
        }

        public static PortalConfig GetById(int configID)
        {
            return (new ExtendedBitPortalEntities()).PortalConfigs.SingleOrDefault(ent => ent.ConfigID == configID);
        }

        public static PortalConfig GetByName(string configName)
        {
            return (new ExtendedBitPortalEntities()).PortalConfigs.SingleOrDefault(ent => ent.Name == configName);
        }

        public static object GetValue(this PortalConfig config)
        {
            string value = config.Value;

            if (!config.IsGlobalConfig)
            {
                ConfigsInLanguage cil = config.ConfigsInLanguages.SingleOrDefault(ent => ent.LanguageCode == BitContext.Current.Lang.LanguageCode);
                if (cil != null && !string.IsNullOrEmpty(cil.Value))
                    value = cil.Value;
            }
            switch (config.DataType)
            {
                case "String":
                    return value;
                case "Text":
                    return value;
                case "Interger":
                    return Convert.ToInt64(value);
                case "Double":
                    return Convert.ToDouble(value);
                case "Datetime":
                    return Convert.ToDateTime(value);
                case "Boolean":
                    return Convert.ToBoolean(value);
            }
            return value;
        }

        public static int SetValue(this PortalConfig config, ExtendedBitPortalEntities bitEnt, string value)
        {
            if (config.IsGlobalConfig)
            {
                config.Value = value;
            }
            else
            {
                ConfigsInLanguage cil = config.ConfigsInLanguages.SingleOrDefault(ent => ent.LanguageCode == BitContext.Current.Lang.LanguageCode);
                if (cil == null)
                {
                    cil = new ConfigsInLanguage() { LanguageCode = BitContext.Current.Lang.LanguageCode, ConfigID = config.ConfigID, Value = value };
                    bitEnt.ConfigsInLanguages.AddObject(cil);
                }
                else
                {
                    cil.Value = value;
                }
            }
            return bitEnt.SaveChanges();
        }

        public static object GetValueByName(string name)
        {
            PortalConfig config = GetByName(name);
            return config.GetValue();
        }

        public static int SetValue(string configName, string value)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            PortalConfig config = bitEnt.PortalConfigs.SingleOrDefault(ent => ent.Name == configName);
            if (config != null)
            {
                config.SetValue(bitEnt, value);
            }
            return 0;
        }

        public static int Delete(string name)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            PortalConfig config = bitEnt.PortalConfigs.SingleOrDefault(ent => ent.Name == name);
            if (config != null)
            {
                bitEnt.PortalConfigs.DeleteObject(config);
                return bitEnt.SaveChanges();
            }
            return 0;
        }

        public static int Delete(int configID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            PortalConfig config = bitEnt.PortalConfigs.SingleOrDefault(ent => ent.ConfigID == configID);
            if (config != null)
            {
                bitEnt.PortalConfigs.DeleteObject(config);
                return bitEnt.SaveChanges();
            }
            return 0;
        }

        public static IQueryable<PortalConfig> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ent in bitEnt.PortalConfigs
                       orderby ent.ConfigID descending
                       select ent;
            return list;
        }

        public static IQueryable<PortalConfig> GetByCondition(string name = null, string dataType = null, bool? allowVisiable = null, bool? allowEdit = null, bool? allowDelete = null, string group = null)
        {
            return GetAll().Where(ent =>
                (name == null || ent.Name.Contains(name)) &&
                (dataType == null || ent.DataType == dataType) &&
                (allowVisiable == null || ent.AllowVisiable == allowVisiable) &&
                (allowEdit == null || ent.AllowEdit == allowEdit) &&
                (allowDelete == null || ent.AllowDelete == allowDelete) &&
                (group == null || ent.Group == group)
                );
        }
    }
}